//
//  FMDBOperationManager.m
//  高仿暴走斗图
//
//  Created by key on 16/10/11.
//  Copyright © 2016年 huangsongyao. All rights reserved.
//

#import "FMDBOperationManager.h"

static FMDBOperationManager *fmdbOperationManager = nil;

@interface FMDBOperationManager ()

@end

@implementation FMDBOperationManager

+ (instancetype)shareInstance {
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        fmdbOperationManager = [[FMDBOperationManager alloc] init];
    });
    return fmdbOperationManager;
}

- (instancetype)init {
    
    if (self = [super init]) {
        
        HSYFMDBOperationInfo *first = [HSYFMDBOperationInfo createDataBaseTableForName:kFMDB_DATABASE_LIST_NAME fields:[[self.class createDatabaseOpeartionFieldInfoForFieldParams:[self.class testTableByFields]] mutableCopy]];

        _fmdbOperation = [[FMDBOperation alloc] initWithDatabaseTables:[@[first] mutableCopy]];
    }
    return self;
    
}

+ (NSMutableArray *)createDatabaseOpeartionFieldInfoForFieldParams:(NSMutableArray <NSDictionary *>*)fieldParams {
    NSMutableArray *infos = [[NSMutableArray alloc] init];
    for (NSDictionary *param in fieldParams) {
        HSYFMDBOperationFieldInfo *operationFieldInfo = [[HSYFMDBOperationFieldInfo alloc] initWithFieldName:param.allKeys.firstObject fieldType:param.allValues.firstObject];
        [infos addObject:operationFieldInfo];
    }
    return infos;
}

+ (HSYFMDBOperationInfo *)createDatabaseOperationInfoForTableName:(NSString *)tableName fieldParams:(NSMutableArray <NSDictionary *>*)params {
    
    HSYFMDBOperationInfo *operation = [HSYFMDBOperationInfo createDataBaseTableForName:tableName fields:[[self.class createDatabaseOpeartionFieldInfoForFieldParams:params] mutableCopy]];
    
    return operation;

}

+ (HSYFMDBOperationInfo *)createDatabaseOperationInfoForTableName:(NSString *)tableName fieldParams:(NSMutableArray <NSDictionary *>*)params insertDatas:(NSMutableArray <NSString *>*)datas {
    HSYFMDBOperationInfo *operation = [HSYFMDBOperationInfo createDataBaseTableForName:tableName fields:[[self.class createDatabaseOpeartionFieldInfoForFieldParams:params] mutableCopy] insertDatas:datas];
    
    return operation;
}

@end


@implementation FMDBOperationManager (Fields)

+ (NSMutableArray <NSDictionary *>*)testTableByFields {
    
    NSMutableArray *params = [@[
                                
                                @{
                                    kFMDB_FIELD_1 : FIELE_TYPE,
                                    },
                                @{
                                    kFMDB_FIELD_2 : FIELE_TYPE,
                                    },
                                @{
                                    kFMDB_FIELD_3 : FIELE_TYPE,
                                    },
//                                @{
//                                    kFMDB_FIELD_4 : FIELE_TYPE,
//                                    },
                                
                                ] mutableCopy];
    return params;
}

@end
